package com.ruoyi.web.controller.user;

import java.util.List;
import java.util.stream.Collectors;
import javax.validation.Valid;

import com.ruoyi.common.base.LocaDataAppVO;
import com.ruoyi.game.domain.GameRole;
import com.ruoyi.game.service.IGameRoleService;
import com.ruoyi.server.domain.ServerInfo;
import com.ruoyi.server.service.IServerInfoService;
import com.ruoyi.user.domain.AppUserAccount;
import com.ruoyi.user.domain.dto.AppUserAccountAddDTO;
import com.ruoyi.user.domain.dto.AppUserAccountPageDTO;
import com.ruoyi.user.domain.dto.AppUserAccountUpdateDTO;
import com.ruoyi.user.service.IAppUserAccountService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 用户游戏账号Controller
 *
 * @author ghostmen
 * @date 2024-11-02
 */
@Validated
@Api(value = "AppUserAccountController对象", tags = "APP用户游戏账号接口")
@RestController
@RequestMapping("/app/user/account")
public class AppUserAccountController extends BaseController {

    @Autowired
    private IAppUserAccountService appUserAccountService;
    @Autowired
    private IGameRoleService gameRoleService;
    @Autowired
    private IServerInfoService serverInfoService;

    /**
     * 查询用户游戏账号列表
     */
    @ApiOperation(value = "查询用户游戏账号列表")
    @GetMapping("/list")
    public TableDataInfo list(AppUserAccountPageDTO pageDTO) {
        pageDTO.setUserId(getLoginAppUser().getUserId());
        startPage();
        List<AppUserAccount> list = appUserAccountService.selectAppUserAccountList(pageDTO);
        return getDataTable(list);
    }


    /**
     * 获取用户游戏账号详细信息
     */
    @ApiOperation(value = "获取用户游戏账号详细信息")
    @GetMapping(value = "/{accountId}")
    public AjaxResult getInfo(@PathVariable("accountId") Long accountId) {
        return success(appUserAccountService.selectAppUserAccountByAccountId(accountId));
    }

    /**
     * 角色职业下拉列表
     */
    @ApiOperation("角色职业下拉列表")
    @GetMapping("/getGameRoleList")
    public TableDataInfo gameRoleList() {
        List<GameRole> list = gameRoleService.getGameRoleList();
        List<LocaDataAppVO> dataAppVOS = list.stream().map(gameRole -> {
            LocaDataAppVO appVO = new LocaDataAppVO();
            appVO.setText(gameRole.getRoleName());
            appVO.setValue(gameRole.getRoleId().toString());
            return appVO;
        }).collect(Collectors.toList());
        return getDataTable(dataAppVOS);
    }


    /**
     * 服务器下拉列表
     */
    @GetMapping("/getServerList")
    @ApiOperation(value = "服务器下拉列表")
    public TableDataInfo getServerList() {
        List<ServerInfo> list = serverInfoService.getServerList();
        List<LocaDataAppVO> dataAppVOS = list.stream().map(serverInfo -> {
            LocaDataAppVO appVO = new LocaDataAppVO();
            appVO.setText(serverInfo.getServer());
            appVO.setValue(serverInfo.getServerId().toString());
            return appVO;
        }).collect(Collectors.toList());
        return getDataTable(dataAppVOS);
    }

    /**
     * 新增用户游戏账号
     */
    @ApiOperation(value = "新增用户游戏账号")
    @PostMapping
    public AjaxResult add(@Valid @RequestBody AppUserAccountAddDTO addDTO) {
        return toAjax(appUserAccountService.insertAppUserAccount(getLoginAppUser().getAppUser(), addDTO));
    }

    /**
     * 修改用户游戏账号
     */
    @ApiOperation(value = "修改用户游戏账号")
    @PutMapping
    public AjaxResult edit(@Valid @RequestBody AppUserAccountUpdateDTO updateDTO) {
        return toAjax(appUserAccountService.updateAppUserAccount(getLoginAppUser().getAppUser(), updateDTO));
    }

    /**
     * 删除用户游戏账号
     */
    @ApiOperation(value = "删除用户游戏账号")
    @GetMapping("/remove")
    public AjaxResult remove(@RequestParam("accountId") Long accountId) {
        return toAjax(appUserAccountService.deleteAppUserAccountByAccountId(accountId));
    }
}
